import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-09-23
 * Time: 23:25
 */
public class min_NumberOfFrogs {
    public int minNumberOfFrogs(String croakOfFrogs) {

        int[] arr = new int[5];
        String str = "croak";
        int n = str.length();
        HashMap<Character, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < n; i++) {
            hashMap.put(str.charAt(i), i);
        }

        char[] chars = croakOfFrogs.toCharArray();
        for (char ch : chars) {
            if (ch == str.charAt(0)) {
                if (arr[n - 1]!=0){
                    arr[n-1]--;
                }
                arr[0]++;
            }else {
                int tmp=hashMap.get(ch);
                if (arr[tmp-1]==0){
                    return -1;
                }
                arr[tmp-1]--;
                arr[tmp]++;
            }
        }

        for(int i = 0; i < n - 1; i++)
            if(arr[i] != 0)
                return -1;

        return arr[n - 1];
    }
}
